class ShimInputsController < ApplicationController
  before_filter :authorize
  before_filter :find_shim
  
  def new
    @shim_input = ShimInput.new
  end
  
  def edit
    @shim_input = @shim.shim_inputs.find(params[:id])
  end
  
  def create
    @shim_input = ShimInput.new(params[:shim_input])    
    if (@shim.shim_inputs << @shim_input)
      @shim_input.create_annotations({"description" => [params[:description]]}, current_user)
      redirect_to shim_url(@shim)
    else
      render :action => :new
    end
  end
  
  def update
    @shim_input = @shim.shim_inputs.find(params[:id])
    if (@shim_input.update_attributes(params[:shim_input]))
      redirect_to shim_url(@shim)
    else
      render :action => :edit
    end
  end
  
  def destroy
    ServiceLink.destroy_all("port_type='ShimInput' AND port_id=#{params[:id]}")
    
    @shim_input = @shim.shim_inputs.find(params[:id])
    @shim.shim_inputs.delete(@shim_input)
    redirect_to shim_url(@shim)
  end
  
  
  
  protected
  
  def authorize 
    user = User.find_by_id(session[:user_id])
    unless user # NOT LOGGED IN
      session[:original_uri] = request.request_uri
      flash[:notice] = "Please log in" 
      redirect_to :controller => "home", :action => "login"
    end 
  end
  
  
  
  private
  
  def find_shim
    @shim_id = params[:shim_id]
    return(redirect_to(shims_url)) unless @shim_id
    @shim = Shim.find(@shim_id)
  end
end
